// components/Headers/Headers.js
import { likeOrZanConference } from '../../utils/getData.js'
const getopen = require('../../utils/maxutil.js');
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    types: { //来源：1:箭头分享头像,2箭头头像,3头像关闭图标,4头像关闭,5支付成功关闭,6有分享图标
      type: String,
      value: 0,
      observer: function(newVal, oldVal) {
        if (newVal != oldVal) {
          this.setData({
            headerStatus:newVal,
            headerTop:getApp().globalData.headerTop
          })
        }
      }
    },
    source: { //来源：1是活动详情页，2个人主页, 3局汇总
      type: String,
      value: 0,
      observer: function(newVal, oldVal) {
        if (newVal !== oldVal) {
          console.log(222222)
          // console.log(app.globalData.conventionInfo.headerdata)
          this.setData({
            source:newVal,
            // headerData:app.globalData.conventionInfo.headerdata
          })
        }
      }
    },
    data: { //
      type: Object,
      value: null,
      observer: function(newVal, oldVal) {
        if(newVal != null){
          console.log(444)
          console.log(newVal)
          this.setData({
            headerData:newVal,
          })
          
        }
        // this.setData({
        //   islike:newVal.islike
        // })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    minModel:app.globalData.minModel, //小机型iphone8
    sponsorId:'', //活动创建者id
    conventionId:'', //活动id
    source:1, //1是活动详情页，2个人主页, 3局汇总, 4服务通知
    islike:false, //是否喜欢该局
    headerStatus:1, //

    sharePopover:false, //分享层的状态

    headerData:app.globalData.conventionInfo.headerdata, //

    headerTop:0, //导航距离顶部的距离
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //返回
    back(){
      const { headerStatus } =this.data;

      if(headerStatus ==2){
        //触发关闭回调
        this.triggerEvent("cancelEvent")
      }else if(headerStatus == 4){
        this.goHome();
      }else {
        wx.navigateBack({
          delta: 1
        })
      }
    },

    //喜欢
    heart(event){
      console.log(333)
      let than = this;
      const { islike } = than.data;
      let conventionid = event.currentTarget.dataset.conventionid;
      let data = {
        conferenceId:conventionid,
        conferenceType:1, //1-活动 2-局
        type:1, //1-喜欢 2-赞
        userId:wx.getStorageSync('userId'),
      }
      likeOrZanConference(data).then(res => {
        if(res.success){
          than.setData({
            islike:islike? false:true
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 1000
          })
        }
      })
    },

    //关闭分享弹出层
    share(){
      this.setData({
        sharePopover:true
      })
    },

    //关闭分享弹出层
    closeShare(){
      this.setData({
        sharePopover:false
      })
    },

    //头像事件
    gomyinfo(event){
      const { sponsorId } = this.data;
      wx.navigateTo({
        url: '../homepage/homepage?userid='+sponsorId
      })

    },
    
    //关闭事件
    close(){
      const { source, data } = this.data;
      console.log('关闭'+source)
      
      //1是活动详情页，2个人主页, 3局汇总
      switch (source) {
        case 1:
          wx.navigateTo({
            url: '../homepage/homepage?userid='+data.sponsorId
          })
          break;
      
        default:
          //触发关闭回调
          this.triggerEvent("cancelEvent")
          break;
      }
    },
    //
    goHome(){
      wx.reLaunch({
        url: '/pages/czqhome/czqhome'
      })
    },

  //分享图片下载到本地
  picDownload(e){
    const { headerData } = this.data;
    let than = this;
    
    wx.downloadFile({
      url: headerData.shareImg,
      success(res) { //下载到本地
        let tempFilePath = res.tempFilePath
        
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath, // 此为图片路径
          success: (res) => {
            than.closeShare();
            wx.showToast({
              title: '图片已保存到相册',
              icon: 'none',
              duration: 1000
            })
          },
          fail: (err) => {
              wx.showToast({
                title: '保存失败，请稍后重试',
                icon: 'none',
                duration: 1000
              })
          }
      })
        
      }
    })
  },
  openApp(){
    //是否安卓机
    if(!app.globalData.isiPhone){
      wx.navigateTo({
        url: '../androidMessage/androidMessage'
      })
      return false
    }
    this.setData({
      goAppPopover: true
    })
  }
  
  },

})


 